<?php
/*
 *   This file is part of poat.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 */

class messages {

	var $id;
	var $status;
	var $teamid;
	var $team;
	var $showstart;
	var $showend;
	var $showstartf;
	var $showendf;
	var $message;
	
    public function __construct()
    {
    }

	// THIS CREATE A NEW INSTANCE OF THIS CLASS
	public function newInstance() {
		//$class = new ReflectionClass('messages');	
		//return $class->newInstance();
		return new messages();
	}

	// COMMON PUBLIC FUNCTIONS // NEEDED FOR LOADER
	public function get() {
		$sql = "SELECT * FROM ".MESSAGES." WHERE id = ".util::str($this->id);
		return $sql;
	}

	public function delete() {
		$sql = "DELETE FROM ".MESSAGES." WHERE id = ".util::str($this->id);
		return $sql;
	}
	
	public function listAll() {
		$sql = "SELECT * FROM ".MESSAGES;
		return $sql;
	}

	public function listMessages() {
		$sql = "SELECT * FROM ".MESSAGES;
		$sql .= " WHERE status = ".util::str(STATUS_ACTIVE)." AND (teamid =".$this->teamid." OR teamid = '0') AND showstart<=".util::str(util::now())." AND showend>=".util::str(util::now())."";
		return $sql;
	}
	
	public function listByTeam() {
		$sql = "SELECT * FROM ".MESSAGES;
		$sql .= " WHERE teamid =".$this->teamid;
		return $sql;
	}
		
	public function insert() {
      $sql = "INSERT INTO ".MESSAGES." (status, teamid, showstart, showend, message) ";
      $sql .= "VALUES (".util::str(STATUS_ACTIVE).",".util::str($this->teamid).",";
      $sql .= util::str($this->showstart);
	  $sql .= ",".util::str($this->showend);
	  $sql .= ",".util::str(util::encode($this->message));
	  $sql .= ")";
	  return $sql;
	}

	public function update() {
      $sql = "UPDATE ".MESSAGES;
      $sql .= " set status = ".util::str($this->status);
      $sql .= ", showstart = ".util::str($this->showstart);
      $sql .= ", showend = ".util::str($this->showend);
      $sql .= ", message = ".util::str(util::encode($this->message));
      $sql .= ", teamid = ".util::str($this->teamid);
      $sql .= " WHERE id = ".util::str($this->id);
	  return $sql;
	}

	public function fill($array) {
		$this->id=$array['id'];
		$this->status=$array['status'];
		$this->teamid=$array['teamid'];
		$this->message=util::decode($array['message']);
		$this->showstart= $array['showstart'];
		$this->showend=$array['showend'];
		$this->showstartf=util::mysqlToDate('Y-m-d',$array['showstart']);
		$this->showendf=util::mysqlToDate('Y-m-d',$array['showend']);
	}

}

?>
